package edu.jsnu.purchase.ticket.controller.ticket;

import dto.resDto.JsonData;
import edu.jsnu.purchase.ticket.controller.common.AbstractController;
import edu.jsnu.purchase.ticket.service.FilmService;
import edu.jsnu.purchase.ticket.service.ScheduleService;
import edu.jsnu.service.ticket.film.dto.FilmBrief;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("ticket/film")
public class FilmController extends AbstractController {

    @Autowired
    private FilmService filmService;

    @Autowired
    private ScheduleService scheduleService;

    @RequestMapping("/schedule.page")
    public String schedule() {
        return "ticket/schedule";
    }

    /*@RequestMapping(value = "/filmList.html", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map getFilmList(String filmType, String curPage, String filmYears){

        System.out.println("to get films...");
        return filmService.getFilmListByTypeOrYears(filmType, curPage, filmYears);
    }*/
    @RequestMapping(value = "/film.do", method = RequestMethod.GET)
    @ResponseBody
    public JsonData filmData(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                             @RequestParam(value = "pageCount", required = false, defaultValue = "30") Integer pageCount,
                             @RequestParam(value = "flag", required = false, defaultValue = "1") Integer flag,
                             @RequestParam(value = "kind", required = false) String kind,
                             @RequestParam(value = "year", required = false) String year) {
        try {
            if (kind != null) {
                //解码
                if (!kind.equals("")) {
                    kind = new String(Base64Utils.decode(kind.getBytes("utf-8")), "utf-8");
                    if (kind.equals("全部")) {
                        kind = null;
                    }
                }
            }
            if (year != null) {
                if (!year.equals("")) {
                    year = new String(Base64Utils.decode(year.getBytes("utf-8")), "utf-8");
                    if (year.equals("全部")) {
                        year = null;
                    }
                }
            }
            List<FilmBrief> filmList = filmService.getBriefFilms(page, pageCount, flag, kind, year);
            return JsonData.success(filmList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonData.fail("数据异常，请联系开发者");

    }

//    public JsonData getFilmList(String filmType, String curPage, String filmYears) throws ParseException {
//        System.out.println("to get films...");
//        Map map = filmService.getFilmListByTypeOrYears(filmType, curPage, filmYears);
//        // ((List<Film>)map.get("filmType")).get(0)
//
//        Schedule schedule = new Schedule();
//        schedule.setStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2019-03-08 11:15:13"));
//        schedule.setEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2019-03-08 14:03:13"));
//        schedule.setPlace(1);
//
//        scheduleService.getFileTypesetting(schedule, true);
//
//        return JsonData.success(map);
//    }
}
